package com.yxkj.syh.app.huarong.activities.user.car;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.syh.app.basic.base.BaseActivity;
import com.syh.app.basic.utils.ComUtil;
import com.syh.app.basic.views.ImageWatcher;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.bean.CarInfo;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.ActivityDriverCarInfoBinding;
import com.yxkj.syh.app.huarong.util.UserManager;

/**
 * 车辆信息
 */
@Route(path = ArouterPath.DRIVER_CAR_INFO_ACTIVITY)
public class DriverCarInfoActivity extends BaseActivity<ActivityDriverCarInfoBinding, DriverCarInfoVM> {

    @Override
    protected int getContentLayout() {
        return R.layout.activity_driver_car_info;
    }

    @Override
    protected int getViewModelId() {
        return BR.driverCarInfoVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mVDBinding.titleView.setLeftOnClickListener(v -> finish());

        mVDBinding.ivCarXsz.setOnClickListener(v -> {
            if (mViewModel.mldCarInfo.getValue() != null && !TextUtils.isEmpty(mViewModel.mldCarInfo.getValue().getCarLicense())) {
                new ImageWatcher(DriverCarInfoActivity.this).show(mViewModel.mldCarInfo.getValue().getCarLicense());
            }
        });
        mVDBinding.ivCarNjd.setOnClickListener(v -> {
            if (mViewModel.mldCarInfo.getValue() != null && !TextUtils.isEmpty(mViewModel.mldCarInfo.getValue().getInspection())) {
                new ImageWatcher(DriverCarInfoActivity.this).show(mViewModel.mldCarInfo.getValue().getInspection());
            }
        });
        mVDBinding.ivCarBxd.setOnClickListener(v -> {
            if (mViewModel.mldCarInfo.getValue() != null && !TextUtils.isEmpty(mViewModel.mldCarInfo.getValue().getCommercialInsurance())) {
                new ImageWatcher(DriverCarInfoActivity.this).show(mViewModel.mldCarInfo.getValue().getCommercialInsurance());
            }
        });
        mVDBinding.ivCarJqx.setOnClickListener(v -> {
            if (mViewModel.mldCarInfo.getValue() != null && !TextUtils.isEmpty(mViewModel.mldCarInfo.getValue().getCompulsoryInsurance())) {
                new ImageWatcher(DriverCarInfoActivity.this).show(mViewModel.mldCarInfo.getValue().getCompulsoryInsurance());
            }
        });
    }

    @Override
    protected void initData() {
        mViewModel.carInfo(UserManager.getUserManager().getUser().getCarId() + "");
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldCarInfo.observe(this, carInfo -> {
            if (carInfo != null) {

                mViewModel.ofForm1.set(carInfo.getName());
                mViewModel.ofForm2.set(carInfo.getNo());
                mViewModel.ofForm3.set(ComUtil.clearNoUseZero(carInfo.getLoadWeight()).toPlainString() + "吨");
                mViewModel.ofForm4.set(carInfo.getMileage() + "km");

                Glide.with(DriverCarInfoActivity.this).load(carInfo.getCarLicense()).into(mVDBinding.ivCarXsz);
                mViewModel.ofForm5.set("有效期：" + carInfo.getCarLicenseIndate());

                Glide.with(DriverCarInfoActivity.this).load(carInfo.getInspection()).into(mVDBinding.ivCarNjd);
                mViewModel.ofForm6.set("有效期：" + carInfo.getInspectionIndate());

                Glide.with(DriverCarInfoActivity.this).load(carInfo.getCommercialInsurance()).into(mVDBinding.ivCarBxd);
                mViewModel.ofForm7.set("有效期：" + carInfo.getCommercialInsuranceIndate());

                Glide.with(DriverCarInfoActivity.this).load(carInfo.getCompulsoryInsurance()).into(mVDBinding.ivCarJqx);
                mViewModel.ofForm8.set("有效期：" + carInfo.getCompulsoryInsuranceIndate());

            }
        });
    }
}
